/**
 * @author Daniel Monteiro
 * */

package bzk3.geometry;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;

import android.util.Log;
import br.odb.libscene.Color;
import br.odb.libscene.Material;
import br.odb.libstrip.AbstractSquare;
import br.odb.libstrip.IndexedSetFace;
import br.odb.utils.Utils;
import br.odb.utils.math.Vec3;

//import android.opengl.GLES20;

public class GLES1Square implements AbstractSquare, GLESIndexedSetFace {

	public float[] color;
	public FloatBuffer colorBuffer;
	private float offset;

	public boolean outline;

	public FloatBuffer vertexBuffer;
	public float[] vertices;
	private boolean visible = true;

	// ------------------------------------------------------------------------------------------------------------
	public GLES1Square() {
		vertices = new float[18];
		color = new float[4];
		
	}

	@Override
	public void addIndex(int index) {
	}

	@Override
	public void addVertex(Vec3 v) {
		// TODO Auto-generated method stub

	}

	// ------------------------------------------------------------------------------------------------------------
	@Override
	public void draw(GL10 gl) {
		
		if ( visible ) {
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
			gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
			gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.length / 3);
			}

		// if ( outline ) {
		//
		// gl.glColor4f( 1.0f, 0.0f, 0.0f, 1.0f );
		// gl.glDisableClientState(GL10.GL_COLOR_ARRAY );
		// gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, vertices.length / 3);
		// gl.glEnableClientState(GL10.GL_COLOR_ARRAY );
		// outline = false;
		// }
		//visible = false;
	}

	public void flushToGLES() {

		// ByteBuffer byteBuf = ByteBuffer.allocateDirect( vertices.length * 4
		// );
		// byteBuf.order( ByteOrder.nativeOrder() );
		// vertexBuffer = byteBuf.asFloatBuffer();
		// vertexBuffer.put( vertices );
		// vertexBuffer.position(0);
		//
		// byteBuf = ByteBuffer.allocateDirect( color.length * 4);
		// byteBuf.order(ByteOrder.nativeOrder());
		// colorBuffer = byteBuf.asFloatBuffer();
		// colorBuffer.put( color );
		// colorBuffer.position(0);
	}

	@Override
	public br.odb.libscene.Color getColor() {

		return new br.odb.libscene.Color(color[0], color[1], color[2]);
	}

	// ------------------------------------------------------------------------------------------------------------

	@Override
	public float getColor(int i) {
		return color[i] + offset;
	}

	@Override
	public float[] getColorData() {

		return color;
	}

	//
	// public void drawGLES2( int vertexHandle, int colorHandle ) {
	// GLES20.glVertexAttribPointer( vertexHandle, 3, GLES20.GL_FLOAT, false, 0,
	// vertexBuffer );
	// GLES20.glEnableVertexAttribArray( vertexHandle );
	// GLES20.glVertexAttribPointer( colorHandle, 4, GLES20.GL_FLOAT, false, 0,
	// colorBuffer );
	// GLES20.glEnableVertexAttribArray( colorHandle );
	//
	// GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4 );
	// }

	@Override
	public int getIndex(int d) {

		return d;
	}

	/**
	 * @return the offset
	 */
	public float getOffset() {
		return offset;
	}

	@Override
	public int getTotalIndexes() {
		return 4;
	}

	@Override
	public Vec3 getVertex(int c) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public float[] getVertexData() {

		return vertices;
	}

	@Override
	public boolean getVisibility() {

		return visible;
	}

	@Override
	public IndexedSetFace makeCopy() {

		GLES1Square s = new GLES1Square();
		System.arraycopy(vertices, 0, s.vertices, 0, vertices.length);

		ByteBuffer byteBuf = ByteBuffer.allocateDirect(s.vertices.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		s.vertexBuffer = byteBuf.asFloatBuffer();
		s.vertexBuffer.put(s.vertices);
		s.vertexBuffer.position(0);

		System.arraycopy(color, 0, s.color, 0, color.length);

		byteBuf = ByteBuffer.allocateDirect(s.color.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		s.colorBuffer = byteBuf.asFloatBuffer();
		s.colorBuffer.put(s.color);
		s.colorBuffer.position(0);

		return s;
	}

	@Override
	public void setColor(br.odb.libscene.Color c) {
		this.color[0] = c.getA();
		this.color[1] = c.getR();
		this.color[2] = c.getG();
		this.color[3] = c.getB();
		
//		color[ 4 ] = color[ 0 ];
//		color[ 5 ] = color[ 1 ];
//		color[ 6 ] = color[ 2 ];
//		color[ 7 ] = color[ 3 ];
	}

	// ------------------------------------------------------------------------------------------------------------
	public void setColor(float R, float G, float B, float A) {
		color[0] = R;
		color[1] = G;
		color[2] = B;
		color[3] = A;

//		color[4] = R;
//		color[5] = G;
//		color[6] = B;
//		color[7] = A;

		

		ByteBuffer byteBuf = ByteBuffer.allocateDirect(color.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		colorBuffer = byteBuf.asFloatBuffer();
		colorBuffer.put(color);
		colorBuffer.position(0);

	}

	public void setColor(int i, float Color) {
		// this.color[i] = Color;
	}

	/**
	 * @param offset
	 *            the offset to set
	 */
	public void setOffset(float offset) {
		this.offset = offset;
	}

	@Override
	public void setVisibility(boolean visibility) {
		visible = visibility;
	}

	// ------------------------------------------------------------------------------------------------------------
	@Override
	public String toString() {
		String toReturn = new String("");
		toReturn += String.valueOf(this.vertices[0]);
		toReturn += ",";
		toReturn += String.valueOf(this.vertices[1]);
		toReturn += ",";
		toReturn += String.valueOf(this.vertices[2]);
		toReturn += ",";
		toReturn += String.valueOf(this.vertices[3]);
		toReturn += ",";
		toReturn += String.valueOf(this.vertices[4]);
		toReturn += ",";
		toReturn += String.valueOf(this.vertices[5]);

		toReturn += "-";
		toReturn += String.valueOf(this.color[0]);
		toReturn += ",";
		toReturn += String.valueOf(this.color[1]);
		toReturn += ",";
		toReturn += String.valueOf(this.color[2]);
		toReturn += ",";
		toReturn += String.valueOf(this.color[3]);

		return toReturn;
	}

	public void setColorWithOffset(Color color2, float candelas) {
		
		float R = Utils.clamp( ( color2.getR() + candelas ), 0.0f, 255.0f ) / 255.0f ;
		float G = Utils.clamp( ( color2.getG() + candelas ), 0.0f, 255.0f ) / 255.0f ;
		float B = Utils.clamp( ( color2.getB() + candelas ), 0.0f, 255.0f ) / 255.0f ;
		float A = Utils.clamp( ( color2.getA() + candelas ), 0.0f, 255.0f ) / 255.0f ;
		
		color[0] = R;
		color[1] = G;
		color[2] = B;
		color[3] = A;

//		color[4] = R;
//		color[5] = G;
//		color[6] = B;
//		color[7] = A;

		

//		ByteBuffer byteBuf = ByteBuffer.allocateDirect(color.length * 4);
//		byteBuf.order(ByteOrder.nativeOrder());
//		colorBuffer = byteBuf.asFloatBuffer();
//		colorBuffer.put(color);
//		colorBuffer.position(0);		
	}

	@Override
	public Material getMaterial() {
		// TODO Auto-generated method stub
		return null;
	}
}
